package com.itheima.controller;

import com.itheima.project.costomannotation.log;
import com.itheima.project.Dept;
import com.itheima.project.Result;
import com.itheima.filter.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 这个类一般负责就收请求响应数据
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptServiceImpl deptService ;//= new DeptServiceImpl();

    //处理不同和请求方式的不同写法
  //1、@RequestMapping(value = "/depts",method = RequestMethod.GET)
  //2  @PostMapping("/depts")

    @GetMapping
    public Result queyrDept() throws IOException {
        //1. 调用deptService
        List<Dept> deptList = deptService.list();
        //2. 响应数据

        return Result.success(deptList);

    }
    @log
    @DeleteMapping
    public Result delete(Integer id){
                //注意这里用了@RequestParam("id")之后必须要传值，不然会报错
        //也可以写成这个样子，把required的默认值修改成false，但是这样写与直接不写效果一样
      //  public Result delete( @RequestParam(required= false,value ="id") Integer id){


        System.out.println("根据id删除部门"+ id);
        deptService.delectDeptById(id);
        return Result.success();

    }
    @log
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        System.out.println("新增部门"+dept);
        deptService.addDept(dept);
        return Result.success();
    }



    //修改表格内容，这两个要一起用，回显
    @GetMapping("/{id}")
    public Result queryDeptById(@PathVariable Integer id){
        System.out.println("根据id查询部门"+id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result modifyDept(@RequestBody Dept dept){
        System.out.println("修改部门名称"+dept);
        deptService.modifyDept(dept);
        return Result.success();
    }

}

